﻿using CSuperSocket.SocketBase.Protocol;
using SuperSocket.ProtoBase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using WebSvrUpdaterCore.Models;
using Newtonsoft.Json;
namespace WebSvrUpdaterCore
{
    public class MsgReceiveFilter : BeginEndMarkReceiveFilter<MsgPacket>
    {
        private readonly static byte[] BeginMark = new byte[] { (byte)'!' };
        private readonly static byte[] EndMark = new byte[] { (byte)'$' };
        public MsgReceiveFilter() : base(BeginMark, EndMark)
        { }        

        public override MsgPacket ResolvePackage(IBufferStream bufferStream)
        {
            var len = (int)bufferStream.Length;
            //var str = bufferStream.ReadString(len, Encoding.UTF8);
            if (len > 0)
            {
                var buffer = new byte[len];

                var startIdx = BeginMark.Length;
                var count = len - BeginMark.Length - EndMark.Length;
                bufferStream.Read(buffer, 0,len);
                var buff2 = new byte[count];
                Array.Copy(buffer, startIdx, buff2, 0, count);
                return MsgPacket.Parser(buff2);
            }

            return null;
        }

        public override MsgPacket Filter(BufferList data, out int rest)
        {
            return base.Filter(data, out rest);
        }
    }
}
